<template>
  <div class="audio__wrap">
    <audio controls :src="src" ref="audioPlayer" style="display: none"></audio>
    <div class="self__audio flex" @click="playAudioHandler">
      <div class="audio__trigger flex flex-jus flex-align-center">
        <div class="audio__duration">{{ duration }}"</div>

        <div
          :class="{
            'wifi-symbol': true,
            'wifi-symbol--avtive': isPlaying
          }"
        >
          <div class="wifi-circle third"></div>
          <div class="wifi-circle second"></div>
          <div class="wifi-circle first"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      isPlaying: false,
      duration: ''
    }
  },
  props: {
    src: {
      type: String,
      required: true
    },
    modelValue: {
      type: Boolean,
      required: true
    }
  },
  emits: ['update:modelValue'],
  watch: {
    modelValue: {
      // username 监听输入框输入
      handler() {
        if (!this.value) {
          this.isPlaying = false
          this.$refs.audioPlayer.load()
        }
      }
      // },
      // immediate: true
    }
  },
  methods: {
    playAudioHandler() {
      this.isPlaying = !this.isPlaying
      const player = this.$refs.audioPlayer
      if (this.isPlaying) {
        player.load()
        player.play()
      } else {
        player.pause()
      }
      setTimeout(() => {
        this.isPlaying = false
        this.$emit('update:modelValue', false)
      }, (this.duration ? this.duration : 0) * 1000)
    }
  },
  mounted() {
    const player = this.$refs.audioPlayer
    player.load()
    player.oncanplay = () => {
      this.duration = Math.floor(player.duration)
    }
  }
}
</script>
<style lang="less" scoped>
.audio__wrap {
  .self__audio {
    border-radius: 4px;
    padding-right: 5px;
    background-color: #6ebe89;
    .audio__duration {
      display: inline-block;
      line-height: 32px;
      height: 32px;
      padding-right: 6px;
      color: #888;
    }
    .audio__trigger {
      padding-left: 10px;
      cursor: pointer;
      vertical-align: top;
      line-height: 32px;
      height: 32px;
      width: 100px;
      .wifi-symbol {
        position: relative;
        width: 50px;
        height: 50px;
        box-sizing: border-box;
        overflow: hidden;
        transform: rotate(-225deg) scale(0.5);
        .wifi-circle {
          border: 5px solid rgba(255, 255, 255, 0.5);
          border-radius: 50%;
          position: absolute;
        }

        .first {
          width: 5px;
          height: 5px;
          background: #cccccc;
          top: 45px;
          left: 45px;
        }
        .second {
          width: 25px;
          height: 25px;
          top: 35px;
          left: 35px;
        }
        .third {
          width: 40px;
          height: 40px;
          top: 25px;
          left: 25px;
        }
      }
      .wifi-symbol--avtive {
        .second {
          animation: bounce 1s infinite 0.2s;
        }
        .third {
          animation: bounce 1s infinite 0.4s;
        }
      }
    }
    @keyframes bounce {
      0% {
        opacity: 0; /*初始状态 透明度为0*/
      }
      100% {
        opacity: 1; /*结尾状态 透明度为1*/
      }
    }
  }
}
</style>
